package cn.hfatec.shms.system.homeConfiguration.service;

import java.util.List;
import java.util.HashMap;


import cn.hfatec.shms.enums.KeyTypeEnum;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.roleHome.dao.RoleHomeDao;
import cn.hfatec.shms.utils.KeyGenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.system.homeConfiguration.model.HomeConfiguration;
import cn.hfatec.shms.system.homeConfiguration.dao.HomeConfigurationDao;
import cn.hfatec.shms.system.homeConfiguration.model.HomeConfigurationParams;

import javax.annotation.Resource;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("HomeConfigurationService")
public class HomeConfigurationService {

    public static final String BEAN_ID = "HomeConfigurationService";

    //注入Dao层
    @Resource(name = "HomeConfigurationDao")
    private HomeConfigurationDao homeConfigurationDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    @Resource(name = "RoleHomeDao")
    private RoleHomeDao roleHomeDao;

    /**
     *
     * 查询数据
     *
     * @param homeConfigurationParams
     * @param page
     *
     */
    @Transactional
    public Table queryHomeConfigurationTable(HomeConfigurationParams homeConfigurationParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", homeConfigurationParams);
        return baseDao.queryTableByPage("queryHomeConfiguration", params, page);
    }

    /**
     *
     * 插入数据
     * @param homeConfiguration
     *
     */
    @Transactional
    public void saveHomeConfiguration(HomeConfiguration homeConfiguration) throws KeyGenException{
        if(homeConfiguration.getId() != null) {
            HomeConfiguration result = getHomeConfiguration(homeConfiguration.getId());
            if(result != null) {
                homeConfigurationDao.update(homeConfiguration);
            } else {
                homeConfiguration.setConfId(KeyGenUtils.createKeyByType(KeyTypeEnum.HOME_CONFIGURATION_KEY));
                homeConfigurationDao.insert(homeConfiguration);
            }
        } else {
            homeConfiguration.setConfId(KeyGenUtils.createKeyByType(KeyTypeEnum.HOME_CONFIGURATION_KEY));
            homeConfigurationDao.insert(homeConfiguration);
        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteHomeConfiguration(Integer id) {
        HomeConfiguration homeConfiguration = homeConfigurationDao.getHomeConfigurationById(id);
        roleHomeDao.deleteRoleHomeByConfId(homeConfiguration.getConfId());
        homeConfigurationDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param homeConfigurationList
     *
     */
    @Transactional
    public void deleteHomeConfigurationBatch(List<String> homeConfigurationList) {
        for(String id : homeConfigurationList) {
            deleteHomeConfiguration(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public HomeConfiguration getHomeConfiguration(Integer id) {

        return homeConfigurationDao.getHomeConfigurationById(id);
    }

    /**
     *
     * 查询首页配置列表
     *
     * @param homeConfigurationParams
     * @return
     */
    @Transactional
    public List queryHomeConfigurationList(HomeConfigurationParams homeConfigurationParams){
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", homeConfigurationParams);
        return baseDao.queryList("queryHomeConfiguration", params);
    }
}